package zk.tool.ui;

import com.alibaba.fastjson.JSON;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zk.tool.model.ConnectInfo;
import zk.tool.util.AlertUtil;
import zk.tool.util.StringUtil;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ConnectController extends AbstractController {
    private static final Logger log = LoggerFactory.getLogger(ConnectController.class);

    private Stage stage;

    @FXML
    private TextField connStrIpt;

    @FXML
    private CheckBox useAuthCheckBox;

    @FXML
    private TextField userIpt;

    @FXML
    private PasswordField passwordIpt;

    @FXML
    private Button connectCancelBtn;

    @FXML
    private Button connectConfirmBtn;

    @FXML
    private ProgressIndicator pi;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        log.info("init connection ui");
        this.connStrIpt.setText("localhost:2181");
    }

    public boolean checkConnect() {
        if (StringUtil.isBlank(connStrIpt.getText())) {
            AlertUtil.info("提示", "连接字符串不能为空");
            return false;
        }
        if (!useAuthCheckBox.isSelected()) {
            return true;
        }

        if (StringUtil.isBlank(userIpt.getText())) {
            AlertUtil.info("提示", "用户名不能为空");
            return false;
        }

        if (StringUtil.isBlank(passwordIpt.getText())) {
            AlertUtil.info("提示", "密码不能为空");
            return false;
        }

        return true;
    }

    public ConnectInfo getConnectInfo() {
        return new ConnectInfo(
                connStrIpt.getText().trim(),
                useAuthCheckBox.isSelected(),
                userIpt.getText().trim(),
                passwordIpt.getText().trim()
        );
    }

    @FXML
    public void cancel() {
        log.info("on cancel");
        this.connStrIpt.setText("");
        this.useAuthCheckBox.setSelected(false);
        this.close();
    }

    @FXML
    public void connect() {
        log.info("on connect");
        if (!this.checkConnect()) {
            return;
        }

        AppController appController = UIManager.getInstance().<AppController>getController(AppController.class);
        ConnectInfo connectInfo = getConnectInfo();
        try {
            this.startLoading();
            appController.connectToZK(connectInfo);
            this.endLoading();
            appController.initNodeTree();
            this.close();
        } catch (IOException e) {
            AlertUtil.exception("连接失败", e);
            this.endLoading();
        } catch (InterruptedException e) {
            AlertUtil.exception("连接失败", e);
            this.endLoading();
        }
        log.info("connection info: {}", JSON.toJSONString(connectInfo));
    }

    private void close() {
        stage.close();
    }

    private void startLoading() {
        this.connectCancelBtn.setDisable(true);
        this.connectConfirmBtn.setDisable(true);
        this.pi.setVisible(true);
    }

    private void endLoading() {
        this.connectCancelBtn.setDisable(false);
        this.connectConfirmBtn.setDisable(false);
        this.pi.setVisible(false);
    }

    @FXML
    public void authCheckBoxChange() {
        boolean selected = useAuthCheckBox.isSelected();
        log.info("authCheckBoxChanged, value is: " + selected);
        userIpt.setEditable(selected);
        userIpt.setEditable(selected);
        userIpt.clear();
        passwordIpt.clear();
        userIpt.setDisable(!selected);
        passwordIpt.setDisable(!selected);
    }

    public void setParent(Stage stage) {
        this.stage = stage;
    }
}
